const app = getApp();
const util = require('../../../utils/util.js');
const lbbService = require('../../../pages/service/service.js');
const lbbTaskService = require('../service/task-service.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        latitude: '31.25392392549918',
        longitude: '120.75753402740479',
        dataValues: {},
        hosLevelList: [],
        optTypeList: [],
        hosTypeList: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this
        that.getProjectList('hospital_level')
        that.getProjectList('hospital_nature')
        that.getProjectList('hospital_type')
    },
    //获取数据字典值
    getProjectList(code) {
        let that = this
        lbbTaskService.getDictByCode({
            code: code
        }, function (res) {
            if (code == 'hospital_level') {
                res.data.unshift({
                    text: '请选择',
                    value: ''
                })
                that.setData({
                    hosLevelList: res.data,
                })
            } else if (code == 'hospital_nature') {
                res.data.unshift({
                    text: '请选择',
                    value: ''
                })
                that.setData({
                    optTypeList: res.data,
                })
            } else if (code == 'hospital_type') {
                res.data.unshift({
                    text: '请选择',
                    value: ''
                })
                that.setData({
                    hosTypeList: res.data,

                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    //医院名称
    onChangeHosName(e) {
        this.data.dataValues.hosName = e.detail
        this.setData({
            dataValues: this.data.dataValues
        })
    },
    //医院别称
    onChangeHosSecondName(e) {
        this.data.dataValues.hosSecondName = e.detail
        this.setData({
            dataValues: this.data.dataValues
        })
    },
    //医院等级
    onChangeHosLevel(e) {
        this.data.dataValues.hosLevel = e.detail
        this.setData({
            dataValues: this.data.dataValues
        })
    },
    //医院性质
    onChangeOptType(e) {
        this.data.dataValues.optType = e.detail
        this.setData({
            dataValues: this.data.dataValues
        })
    },
    //医院类型
    onChangeHosType(e) {
        this.data.dataValues.hosType = e.detail
        this.setData({
            dataValues: this.data.dataValues
        })
    },
    //地图获取位置
    openMap() {
        let that=this
        wx.chooseLocation({
            latitude: 31.25392392549918,
            longitude: 120.75753402740479,
            success: (res) => {
                that.data.dataValues.address=res.name
                that.data.dataValues.longitude=res.longitude
                that.data.dataValues.latitude=res.latitude
                wx.request({
                    url: 'https://api.map.baidu.com/geocoder/v2/',
                    method: 'GET',
                    data: {
                        location: res.latitude + ',' + res.longitude,
                        output: 'json',
                        coordtype: 'wgs84ll',
                        pois: 1,
                        s: 1,
                        latest_admin: 1,
                        ak: 'GQKwKHdtu8CgmxXIkXdyTkdtlfvML4Gg'
                    },
                    success(res) {
                        if (res.data.status == 0 && res.data.result.formatted_address) {
                            let address = res.data.result.formatted_address
                            that.data.dataValues.adCode=res.data.result.addressComponent.adcode
                            that.setData({
                                dataValues:that.data.dataValues
                            })
                        } else {
                            wx.showToast({
                                title: '获取位置失败,请打开手机定位重新签到',
                                icon: 'none',
                                duration: 3000
                            })
                        }
                    },
                    fail() {
                        that.setData({
                            address: null,
                        })
                    }
                })
            }
        })
    },
    //提交
    btnSubmit() {
        let that = this
        let obj=JSON.parse(JSON.stringify(that.data.dataValues))
        console.log(obj)
        if(obj.hosName&&obj.hosSecondName&&obj.address&&obj.adCode&&obj.hosLevel&&obj.hosType&&obj.optType){
            lbbTaskService.addMechanism({...obj},function(res){
                wx.navigateBack({
                    delta: 1
                  })
        })
        }else{
            that.openPrompt()   
        }
    },
     //校验提示
     openPrompt() {
        wx.showToast({
            title: `请检查必填项`,
            icon: 'error',
            duration: 2000
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})